<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package}.dao.${table.className}Mapper">

    <select id="get${table.className}ById" resultType="${table.className}">
        select
    <#list table.columns as column>
        <#if column_has_next>
        ${column.columnName} as ${column.fieldName},
        <#else>
        ${column.columnName} as ${column.fieldName}
        </#if>
    </#list>
        from ${table.tableName}
        <trim prefix="where" prefixOverrides="and | or">
            <if test="id != null">
                and id=${r"#{"}id}
            </if>
        </trim>
    </select>

    <select id="get${table.className}ListByMap" resultType="${table.className}" parameterType="java.util.Map">
        select   <#list table.columns as column>
        <#if column_has_next>
        ${column.columnName} as ${column.fieldName},
        <#else>
        ${column.columnName} as ${column.fieldName}
        </#if>
    </#list> from ${table.tableName}
        <trim prefix="where" prefixOverrides="and | or">
        <#list table.columns as column>
            <if test="${column.fieldName} != null and ${column.fieldName}!=''">
                and ${column.columnName}=${r"#{"}${column.fieldName}}
            </if>
        </#list>
        </trim>
        order by creationDate desc
        <if test="beginPos != null and beginPos!='' and pageSize != null  and pageSize !='' ">
            limit ${r"#{"}beginPos},${r"#{"}pageSize}
        </if>
    </select>

    <select id="get${table.className}CountByMap" resultType="Integer" parameterType="java.util.Map">
        select count(*) from ${table.tableName}
        <trim prefix="where" prefixOverrides="and | or">
        <#list table.columns as column>
            <if test="${column.fieldName} != null and ${column.fieldName}!=''">
                and ${column.columnName}=${r"#{"}${column.fieldName}}
            </if>
        </#list>
        </trim>
    </select>

    <insert id="save${table.className}" parameterType="${table.className}">
        insert into ${table.tableName}(
    <#list table.columns as column>
        <#if column_has_next>
        ${column.columnName},
        <#else >
        ${column.columnName}
        </#if>
    </#list>
        )
        values(
    <#list table.columns as column>
        <#if column_has_next>
        ${r"#{"}${column.fieldName}},
        <#else >
        ${r"#{"}${column.fieldName}}
        </#if>
    </#list>
        )
    </insert>

    <update id="modify${table.className}" parameterType="${table.className}">
        update ${table.tableName}
        <trim prefix="set" suffixOverrides="," suffix="where id=${r"#{"}id}">
        <#list table.columns as column>
            <#if column_has_next>
                <if test="${column.fieldName} != null and ${column.fieldName}!=''">
                ${column.columnName}=${r"#{"}${column.fieldName}},
                </if>
            <#else >
                <if test="${column.fieldName} != null and ${column.fieldName}!=''">
                ${column.columnName}=${r"#{"}${column.fieldName}}
                </if>
            </#if>
        </#list>
        </trim>
    </update>

    <delete id="remove${table.className}ById" parameterType="Long">
        delete from ${table.tableName} where id = ${r"#{"}id}
    </delete>


    <delete id="remove${table.className}ByIds" parameterType="Long">
        delete from ${table.tableName} where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
        ${r"#{"}id}
        </foreach>
    </delete>
</mapper>